package oop.demo.restaurantmanagementsystem;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Service class for restaurant management that provides the API specified in the project requirements.
 * This class acts as a facade for the restaurant billing system functionality.
 */
public class RestaurantService {
    private RestaurantBilling billingSystem;
    private HashMap<String, Order> orders; // To track orders by ID

    public RestaurantService() {
        this.billingSystem = new RestaurantBilling();
        this.orders = new HashMap<>();
    }

    /**
     * Adds an ingredient to the system
     * @param name Name of the ingredient
     * @param price Price of the ingredient
     */
    public void addIngredient(String name, double price) {
        Ingredient ingredient = new Ingredient(name, price);
        // Note: The current system doesn't track ingredients separately,
        // so this method would need to be expanded if ingredient inventory is needed
    }

    /**
     * Creates a meal from a list of ingredient names
     * @param mealName Name of the meal to create
     * @param ingredientNames List of ingredient names to include in the meal
     */
    public void createMeal(String mealName, ArrayList<String> ingredientNames) {
        Meal meal = new Meal(mealName);
        // In a real implementation, we would look up ingredients by name
        // and add them to the meal. For now, we'll just create a meal with
        // placeholder ingredients based on the names.
        for (String ingName : ingredientNames) {
            meal.addIngredient(new Ingredient(ingName, 5.0)); // Default price
        }
        billingSystem.addMeal(meal);
    }

    /**
     * Places an order with specified meal quantities
     * @param mealQuantities Map of meal names to quantities
     * @return Order ID for reference
     */
    public String placeOrder(HashMap<String, Integer> mealQuantities) {
        String orderId = "ORD" + System.currentTimeMillis(); // Simple unique ID
        billingSystem.clearOrder(); // Start fresh for this order

        for (String mealName : mealQuantities.keySet()) {
            int quantity = mealQuantities.get(mealName);
            for (int i = 0; i < quantity; i++) {
                billingSystem.addMealToOrder(mealName);
            }
        }

        orders.put(orderId, new Order(orderId, billingSystem.calculatingBill()));
        return orderId;
    }

    /**
     * Calculates the bill for a given order
     * @param orderId The order ID to calculate
     * @return Total bill amount
     */
    public double calculateBill(String orderId) {
        Order order = orders.get(orderId);
        if (order != null) {
            return order.getTotal();
        }
        return 0.0;
    }

    // Inner class to represent an order
    private class Order {
        private String orderId;
        private double total;

        public Order(String orderId, double total) {
            this.orderId = orderId;
            this.total = total;
        }

        public String getOrderId() {
            return orderId;
        }

        public double getTotal() {
            return total;
        }
    }
}